package com.cloud.pikachu.dingtalk.api;

import com.cloud.pikachu.dingtalk.bean.common.AccessToken;
import com.cloud.pikachu.dingtalk.bean.common.SsoToken;
import com.cloud.pikachu.dingtalk.client.LocalHttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;

/**
 * AccessToken/SsoToken管理
 * @author ABug
 */
public class TokenAPI extends BaseAPI {

	/**
	 * 获取access_token
	 * 注意 : 正常情况下access_token有效期为7200秒，有效期内重复获取返回相同结果，并自动续期。
	 * 集群设计 : 在集群部署环境下可以每个服务上面缓存一份accessToken 并设定7200秒超时重新请求 , 集群之间相互保持 , 互不影响 ,
	 * @param corpid  企业Id
	 * @param corpsecret  企业应用的凭证密钥
	 * @return
	 */
	public static AccessToken accessToken(String corpid, String corpsecret){
		HttpUriRequest httpUriRequest = RequestBuilder.get()
				.setUri(BASE_URI + "/gettoken")
				.addParameter("corpid", corpid)
				.addParameter("corpsecret", corpsecret)
				.build();
		//Content-Type:application/json;charset=UTF-8
		return LocalHttpClient.executeJsonResult(httpUriRequest, AccessToken.class);
	}

	/**
	 * 获取SsoToken，暂未使用
	 * @param corpid  企业Id
	 * @param corpsecret  这里必须填写专属的SSOSecret
	 * @return
	 */
	@Deprecated
	public static SsoToken ssoToken(String corpid, String corpsecret){
		HttpUriRequest httpUriRequest = RequestBuilder.get()
				.setUri(BASE_URI + "/sso/gettoken")
				.addParameter("corpid", corpid)
				.addParameter("corpsecret", corpsecret)
				.build();
		return LocalHttpClient.executeJsonResult(httpUriRequest, SsoToken.class);
	}
}
